<?php

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use utils\out;

/**菜单列表*/
$app->get('/bd/comment', function (Application $app, Request $request) {
	if(!$_SESSION || !$_SESSION['userinfo']) {
		return $app->redirect('/login');
	}
	$limit = 20;
	$page = $request->get('page');
	if (!$page) {
        $page = 1;
    }
	$start = ($page - 1) * $limit;
	
	$sql = "select count(id) from t_comment";
	$data = $app['db']->fetchAssoc($sql);
	$allNum = $data['count(id)'];
	$maxPage = ceil($allNum/$limit);
	
	$sql = "select * from t_comment order by id desc limit $start,$limit";
	$data = $app['db']->fetchAll($sql);

	for($i=0; $i<count($data); $i++) {
		$data[$i]['addtime'] =date("Y-m-d H:i", $data[$i]['addtime']);
	}

	return $app['twig']->render('bd_comment_list.html', array('result'=>$data, 'page'=>$page,'maxpage'=>$maxPage));
});

$app->get('/bd/commentAll',function (Application $app, Request $request) {
	if(!$_SESSION || !$_SESSION['userinfo']) {
		return $app->redirect('/login');
	}
	
	$sql = 'select marketid,count(marketid) as mnum ,sum(star) as star,marketname from t_comment GROUP BY marketid ';
	$data = $app['db']->fetchAll($sql);
	
	foreach($data as $k=>$v){
		$data[$k]['star'] = ($v['star']/($v['mnum']*5))*5;
		$data[$k]['star'] = sprintf("%.1f",$data[$k]['star']);
//		
	}
	
	return $app['twig']->render('bd_commentAll.html', array('result'=>$data));
});

/**删除菜单*/
$app->delete('/bd/deletecomment/{id}', function (Application $app, Request $request, $id) {
	if(!$_SESSION || !$_SESSION['userinfo']) {
		return $app->redirect('/login');
	}
	$sql = "delete from t_comment where id = ?";
	$data = $app['db']->executeUpdate($sql, array($id));

	return $app->json(array("success"=>true, "result"=>"ok"));
});

?>